package com.Txunda.fxw.ui.activity.home;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.fgt.SmallSizeFgt;
import com.Txunda.fxw.ui.http.Index;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class SmallSizeAty extends BaseAty {

    @BindView(R.id.aty_title3_icon)
    ImageView atyTitle3Icon;
    @BindView(R.id.aty_title3_tv_center)
    TextView atyTitle3TvCenter;
    @BindView(R.id.aty_title3_tv_right2)
    TextView atyTitle3TvRight2;
    @BindView(R.id.aty_small_sized_tab)
    TabLayout atySmallSizedTab;
    @BindView(R.id.aty_small_sized_vp)
    ViewPager atySmallSizedVp;
    private String lat = "";
    private String lnt = "";
    private Index index;
    private String area_name = "";
    private int p = 1;
    private List<String> mTitle = new ArrayList<>();
    private List<String> mIds = new ArrayList<>();
    private List<Fragment> mFragment = new ArrayList<>();
    private SmallSizeAdapter adapter;
//    private Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//                case 1:
//
//                    break;
//            }
//        }
//    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        atyTitle3TvCenter.setText("峰行小影");
        atyTitle3TvRight2.setVisibility(View.VISIBLE);
        Log.d("hzyyyy", "smallSizedClassify: ============="+"1111");
        atyTitle3TvRight2.setText(area_name);
        atySmallSizedTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        loadMessage();
    }

    private void loadMessage() {
        HttpRequest.POST(this, "http://app.yiyuanchazuo.com/index.php/Api/Index/fxmovie", new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                smallSizedClassify(map);
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_small_size;
    }

    @Override
    protected void initialized() {
        index = new Index();
        area_name = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_mode);
        // 获取经度纬度
        lat = PreferenceUtil.readString(this, PreferenceUtil.sp_lat);
        lnt = PreferenceUtil.readString(this, PreferenceUtil.sp_lnt);
    }

    @Override
    protected void requestData() {

    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        Log.d("hzyyyy", "smallSizedClassify: ============="+"hhh");
        if (requestUrl.contains("Api/Index/fxmovie")) {
            if (smallSizedClassify(map)) return;
            return;
        }
    }

    @OnClick(R.id.aty_title3_icon)
    public void onViewClicked() {
        finish();
    }
    // 小影分类
    private boolean smallSizedClassify(Map<String, String> map) {
        Log.d("hzyyyy", "smallSizedClassify: ============="+"hhh");
        List<Map<String, String>> smallSized = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
        if (ListUtils.isEmpty(smallSized)) {
            return true;
        }
        p = 1;
        for (int i = 0; i < smallSized.size(); i++) {
            Log.d("hzyyyy", "smallSizedClassify: ============="+smallSized.get(i).get("name"));
            mTitle.add(smallSized.get(i).get("name"));
            mFragment.add(new SmallSizeFgt(smallSized.get(i).get("id")));
        }
        Message message = new Message();
        message.what = 1;
//        handler.sendMessage(message);
        adapter = new SmallSizeAdapter(getSupportFragmentManager(),mFragment,mTitle);
        atySmallSizedVp.setAdapter(adapter);

        atySmallSizedTab.setupWithViewPager(atySmallSizedVp);
        return false;
    }
    class SmallSizeAdapter extends FragmentPagerAdapter{
        private List<Fragment> fragments;
        private List<String> names;

        public SmallSizeAdapter(FragmentManager fm, List<Fragment> fragments, List<String> names) {
            super(fm);
            this.fragments = fragments;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position);
        }
    }
}
